package wxact

import (
	"log"

	"github.com/labstack/echo"
	"myproject/wx_test/src/wxmod"
)

func (*Resp) HandleFunc(ctx echo.Context) error {
	log.Println(ctx.QueryParams())

	var req wxmod.Request
	req.Signature = ctx.QueryParam("signature")
	req.TimeStamp = ctx.QueryParam("timestamp")
	req.Nonce = ctx.QueryParam("nonce")
	echoStr := ctx.QueryParam("echostr")

	if req.Signature == "" && req.TimeStamp == "" && req.Nonce == "" && echoStr == "" {
		log.Println("无任何数据")
		return ctx.String(403, "hello, this is handle view")
	}

	//生成hashcode
	hashcode, err := wxmod.GenHashcode(req)
	if err != nil {
		log.Println("系统错误，genHashcode")
		return ctx.String(500, "系统错误")
	}

	//检查签名是否匹配
	if hashcode != req.Signature {
		//消息来源不是微信
		log.Println("签名验证失败")
		return ctx.String(200, "")
	}
	//返回echostr
	log.Println("签名验证成功")
	return ctx.String(200, echoStr)
}
